package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.ClientUser;
import cn.cs.pojo.UserAddress;
import cn.cs.service.ClientBigUserService;
import cn.cs.service.ClientUserService;
import cn.cs.service.UserAddressService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/clientBigUser")
public class ClientBigUserController {

    @Resource
    private ClientBigUserService clientBigUserService;

    @Resource
    private ClientUserService clientUserService;

    @Resource
    private UserAddressService userAddressService;

    /**
     * 分页查询 大客户
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getClientBigUserByPage")
    public Dto getClientBigUserByPage(Integer pageNumber, Integer pageSize, String userAccount, String userPhone, String companyName) throws Exception {
        Map<String, Object> param = new HashMap<>();
//        param.put("userName", userName);
        param.put("userAccount", userAccount);
        param.put("userPhone", userPhone);
        param.put("companyName", companyName);
        return DtoUtil.returnSuccess("获取成功", clientBigUserService.queryClientBigUserPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 新增/编辑 大客户
     * @param clientUser
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateClientBigUser")
    @Transactional
    public Dto updateClientBigUser(@RequestBody ClientUser clientUser) {
        try {
            String msg = "新增成功";
            UserAddress address = new UserAddress();
            address.setProvince(clientUser.getProvince());
            address.setCity(clientUser.getCity());
            address.setArea(clientUser.getArea());
            address.setAddressContent(clientUser.getAddressContent());
            address.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            if (EmptyUtils.isEmpty(clientUser.getId())) {
                ClientUser user = clientUserService.getClientUserByPhone(clientUser.getPhone());
                if(EmptyUtils.isNotEmpty(user)){
                    return DtoUtil.returnFail("手机号已存在，请更换手机号", "003");
                }
                clientUser.setId(IdWorker.getId());
                clientUser.setUserPassword(MD5Util.encode(clientUser.getUserPassword()));
                String code = String.format("%03d", clientBigUserService.getMaxCode("client_user"));
                clientUser.setUserAccount(PingYinUtil.getFirstSpell(clientUser.getUserName()) + clientUser.getPhone().substring(7, 11) + code);

                address.setId(IdWorker.getId());
                address.setUserId(clientUser.getId());
                address.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                userAddressService.itriptxAddUserAddress(address);
                clientBigUserService.addClientBigUser(clientUser);
            } else {
                ClientUser user = clientUserService.getClientUserByPhone(clientUser.getPhone());
                if(EmptyUtils.isNotEmpty(user) && !clientUser.getId().equals(user.getId())){
                    return DtoUtil.returnFail("手机号已存在，请更换手机号", "003");
                }
                msg = "修改成功";
                if (EmptyUtils.isNotEmpty(clientUser.getUserPassword())) {
                    clientUser.setUserPassword(MD5Util.encode(clientUser.getUserPassword()));
                }
                address.setUserId(clientUser.getId());
                userAddressService.modifyUserAddressByUserId(address);
                clientBigUserService.modifyClientBigUser(clientUser);
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     *  修改密码
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateClientBigUserPassWord")
    @Transactional
    public Dto updateClientBigUserPassWord(String userId, String password)throws Exception {
        ClientUser user = new ClientUser();
        user.setId(userId);
        user.setUserPassword(MD5Util.encode(password));
        return DtoUtil.returnSuccess("操作成功", clientBigUserService.modifyClientBigUser(user));
    }



}
